package ch.subgate.pongwars;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainMenuActivity extends Activity implements OnClickListener {

    private float size;

    private float volume;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu_layout);
        
        // Change textview fonts
        Typeface face = Typeface.createFromAsset(getAssets(), "fonts/ca.ttf");
        TextView tv = (TextView)findViewById(R.id.singleplayer_button);
        tv.setTypeface(face);
        tv = (TextView)findViewById(R.id.multiplayer_button);
        tv.setTypeface(face);
        tv = (TextView)findViewById(R.id.multiplayerbt_button);
        tv.setTypeface(face);
        tv = (TextView)findViewById(R.id.difficulty_button);
        tv.setTypeface(face);
        tv = (TextView)findViewById(R.id.sound_button);
        tv.setTypeface(face);
        
        // Set OnClickListeners
        findViewById(R.id.singleplayer_button).setOnClickListener(this);
        findViewById(R.id.multiplayer_button).setOnClickListener(this);
        findViewById(R.id.multiplayerbt_button).setOnClickListener(this);
        findViewById(R.id.difficulty_button).setOnClickListener(this);
        findViewById(R.id.sound_button).setOnClickListener(this);

        SharedPreferences sharedPrefs = this.getPreferences(Context.MODE_PRIVATE);

        size = Constants.PADDLE_EASY;
        volume = sharedPrefs.getFloat("volume", 1.0f);

        if (volume == 1.0f) { ((TextView)this.findViewById(R.id.sound_button)).setText(">Sound on"); }
        else { ((TextView)this.findViewById(R.id.sound_button)).setText(">Sound off"); }
    } // onCreate

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mainmenu_layout, menu);
        return true;
    }

	@Override
	public void onClick(View v) {
        Intent intent;
        intent = new Intent(MainMenuActivity.this, GameActivity.class);
        intent.putExtra("size", size);
        intent.putExtra("volume", volume);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		switch (v.getId()) {
            case R.id.singleplayer_button:
                intent.putExtra("mode", Constants.MODE_PVE);
                startActivity(intent);
                break;
            case R.id.multiplayer_button:
                intent.putExtra("mode", Constants.MODE_PVP);
                startActivity(intent);
                break;
            case R.id.multiplayerbt_button:
                intent = new Intent(MainMenuActivity.this, LobbyActivity.class);
                intent.putExtra("size", size);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                break;
            case R.id.difficulty_button:
                if (((TextView)v).getText().equals(">Easy")) {
                    size = Constants.PADDLE_MEDIUM;
                    ((TextView)v).setText(">Medium");
                } else if (((TextView)v).getText().equals(">Medium")) {
                    size = Constants.PADDLE_HARD;
                    ((TextView)v).setText(">Hard");
                } else {
                    size = Constants.PADDLE_EASY;
                    ((TextView)v).setText(">Easy");
                } // if
                break;
            case R.id.sound_button:
                if (((TextView)v).getText().equals(">Sound on")) {
                    volume = 0.0f;
                    ((TextView)v).setText(">Sound off");
                } else {
                    volume = 1.0f;
                    ((TextView)v).setText(">Sound on");
                } // if
                SharedPreferences sharedPrefs = this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor e = sharedPrefs.edit();
                e.putFloat("volume", volume);
                e.commit();
                break;
		} // switch
	} // onClick

}
